package cn.amaake.controller;


import com.jfinal.aop.Clear;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.Ret;

import cn.amaake.common.model.User;
import cn.amaake.interceptor.LoginInterceptor;
import cn.amaake.service.UserService;
import cn.amaake.service.UserServiceImp;

@Clear({LoginInterceptor.class, SessionInViewInterceptor.class})
public class LoginController extends Controller {

    @Inject(UserServiceImp.class)
    private UserService userService;

    public void randomcode(){
        renderCaptcha();
    }

    public void index(){
    	boolean resultcode = validateCaptcha("code");
    	if(resultcode) {
	        String username = getPara("username");
	        String password = getPara("password");
	        User user = userService.login(username,password);
	        if(user==null){
	            renderJson(Ret.fail("msg","账号或密码错误，登录失败！"));
	        }else{
	            setSessionAttr("usersession",user);
	            renderJson(Ret.ok("msg","登录成功！"));
	        }
    	}else {
    		renderJson(Ret.fail("msg","验证码错误！"));
    	}
    }

    public void logout(){
        removeSessionAttr("usersession");
        redirect("/admin");
    }
    
    


}
